#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Utilities.Miscellaneous;
using System.Xml;
using Utilities.Testing;
using Utilities.Xml;

namespace Utilities.Test.Xml
{
  public class XmlUtilsFixture : TestFixtureBase
  {
    private XmlDocument _doc;

    public override void FixtureSetup()
    {
      _doc = new XmlDocument();
      _doc.LoadXml(@"<bib>
    <book year=""1992"" joe=""1,2,3"">
        <title>Advanced Programming in the Unix environment</title>
        <author><last>Stevens</last><first>W.</first></author>
        <author><last>Golde</last><first>Peter</first></author>
        <publisher>Addison-Wesley</publisher>
        <price>65.95</price>
    </book>
    <book year=""2000"">
        <title>Data on the Web</title>
        <author><last>Abiteboul</last><first>Serge</first></author>
        <author><last>Buneman</last><first>Peter</first></author>
        <author><last>Suciu</last><first>Dan</first></author>
        <publisher>Morgan Kaufmann Publishers</publisher>
        <price>39.95</price>
    </book>
    <book year=""1999"">
        <title>The Economics of Technology and Content for Digital TV</title>
        <author><last>Hejlsberg</last><first>Anders</first></author>
        <editor>
               <last>Gerbarg</last><first>Darcy</first>
                <affiliation type=""informal"">CITI</affiliation>
        </editor>
            <publisher>Kluwer Academic Publishers</publisher>
        <price>129.95</price>
    </book>
</bib>");
    }

    [Test]
    public void IndentXmlDocument()
    {
      string indentedXml = XmlUtils.IndentXml(_doc, " ");
      Assert.AreEqual(@"<bib>
 <book year=""1992"" joe=""1,2,3"">
  <title>Advanced Programming in the Unix environment</title>
  <author>
   <last>Stevens</last>
   <first>W.</first>
  </author>
  <author>
   <last>Golde</last>
   <first>Peter</first>
  </author>
  <publisher>Addison-Wesley</publisher>
  <price>65.95</price>
 </book>
 <book year=""2000"">
  <title>Data on the Web</title>
  <author>
   <last>Abiteboul</last>
   <first>Serge</first>
  </author>
  <author>
   <last>Buneman</last>
   <first>Peter</first>
  </author>
  <author>
   <last>Suciu</last>
   <first>Dan</first>
  </author>
  <publisher>Morgan Kaufmann Publishers</publisher>
  <price>39.95</price>
 </book>
 <book year=""1999"">
  <title>The Economics of Technology and Content for Digital TV</title>
  <author>
   <last>Hejlsberg</last>
   <first>Anders</first>
  </author>
  <editor>
   <last>Gerbarg</last>
   <first>Darcy</first>
   <affiliation type=""informal"">CITI</affiliation>
  </editor>
  <publisher>Kluwer Academic Publishers</publisher>
  <price>129.95</price>
 </book>
</bib>", indentedXml);
    }

    [Test]
    public void IndentXmlEmptyDocument()
    {
      XmlDocument doc = new XmlDocument();

      string indentedXml = XmlUtils.IndentXml(doc, " ");
      Assert.AreEqual(string.Empty, indentedXml);
    }

    [Test]
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"'xml' cannot be empty.
Parameter name: xml")]
    public void IndentXmlEmptyString()
    {
      XmlUtils.IndentXml(string.Empty, " ");
    }

    #region TryIndentXml
    [Test]
    public void TryIndentXmlNoDeclaration()
    {
      string indentedXml;
      bool result = XmlUtils.TryIndentXml(@"<ROOT><TEST>1</TEST></ROOT>", out indentedXml);

      Assert.AreEqual(@"<ROOT>
  <TEST>1</TEST>
</ROOT>", indentedXml);

      Assert.IsTrue(result);
    }

    [Test]
    public void TryIndentXmlDeclaration()
    {
      string indentedXml;
      bool result = XmlUtils.TryIndentXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
<ROOT><TEST>1</TEST></ROOT>", out indentedXml);

      Assert.AreEqual(@"<?xml version=""1.0"" encoding=""utf-8""?>
<ROOT>
  <TEST>1</TEST>
</ROOT>", indentedXml);

      Assert.IsTrue(result);
    }

    [Test]
    public void TryIndentXmlIndentChars()
    {
      string indentedXml;
      bool result = XmlUtils.TryIndentXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
<ROOT><TEST>1</TEST></ROOT>", "\t", out indentedXml);

      Assert.AreEqual(@"<?xml version=""1.0"" encoding=""utf-8""?>
<ROOT>
	<TEST>1</TEST>
</ROOT>", indentedXml);

      Assert.IsTrue(result);
    }

    [Test]
    public void TryIndentXmlInvalid()
    {
      string indentedXml;
      bool result = XmlUtils.TryIndentXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
<ROOT><TEST>1</TES<T></ROOT>", out indentedXml);

      Assert.IsNull(indentedXml);

      Assert.IsFalse(result);
    }

    [Test]
    public void TryIndentXmlBib()
    {
      string indentedXml;
      bool result = XmlUtils.TryIndentXml(_doc.InnerXml, out indentedXml);

      Assert.IsTrue(result);
      Assert.AreEqual(@"<bib>
  <book year=""1992"" joe=""1,2,3"">
    <title>Advanced Programming in the Unix environment</title>
    <author>
      <last>Stevens</last>
      <first>W.</first>
    </author>
    <author>
      <last>Golde</last>
      <first>Peter</first>
    </author>
    <publisher>Addison-Wesley</publisher>
    <price>65.95</price>
  </book>
  <book year=""2000"">
    <title>Data on the Web</title>
    <author>
      <last>Abiteboul</last>
      <first>Serge</first>
    </author>
    <author>
      <last>Buneman</last>
      <first>Peter</first>
    </author>
    <author>
      <last>Suciu</last>
      <first>Dan</first>
    </author>
    <publisher>Morgan Kaufmann Publishers</publisher>
    <price>39.95</price>
  </book>
  <book year=""1999"">
    <title>The Economics of Technology and Content for Digital TV</title>
    <author>
      <last>Hejlsberg</last>
      <first>Anders</first>
    </author>
    <editor>
      <last>Gerbarg</last>
      <first>Darcy</first>
      <affiliation type=""informal"">CITI</affiliation>
    </editor>
    <publisher>Kluwer Academic Publishers</publisher>
    <price>129.95</price>
  </book>
</bib>", indentedXml);
    }
    #endregion

    #region CreateAttribute
    [Test]
    public void CreateAttribute()
    {
      XmlAttribute attribute = XmlUtils.CreateAttribute(_doc, "TestAttributeName", "TestAttributeValue");
      Assert.IsNotNull(attribute);
      Assert.AreEqual(_doc, attribute.OwnerDocument);
      Assert.AreEqual("TestAttributeName", attribute.Name);
      Assert.AreEqual("TestAttributeValue", attribute.Value);
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: doc")]
    public void CreateAttributeNullDocument()
    {
      XmlUtils.CreateAttribute(null, "TestAttributeName", "TestAttributeValue");
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: name")]
    public void CreateAttributeNullName()
    {
      XmlUtils.CreateAttribute(_doc, null, "TestAttributeValue");
    }

    [Test]
    [ExpectedException(typeof(ArgumentException), ExpectedMessage = @"'name' cannot be empty.
Parameter name: name")]
    public void CreateAttributeEmptyName()
    {
      XmlUtils.CreateAttribute(_doc, string.Empty, "TestAttributeValue");
    }

    [Test]
    public void CreateAttributeNullValue()
    {
      XmlUtils.CreateAttribute(_doc, "TestAttributeName", null);
    }
    #endregion

    #region ToXmlDocument
    [Test]
    public void ToXmlDocumentString()
    {
      XmlDocument doc = XmlUtils.ToXmlDocument("Pie");
      Assert.IsNotNull(doc);
      Assert.AreEqual("<string>Pie</string>", doc.InnerXml);
    }

    [Test]
    [ExpectedException(typeof(ArgumentNullException), ExpectedMessage = @"Value cannot be null.
Parameter name: value")]
    public void ToXmlDocumentNull()
    {
      XmlUtils.ToXmlDocument(null);
    }

    [Test]
    public void ToXmlDocumentInt()
    {
      XmlDocument doc = XmlUtils.ToXmlDocument(10);
      Assert.IsNotNull(doc);
      Assert.AreEqual("<int>10</int>", doc.InnerXml);
    }

    public class TestToXmlDocument
    {
      private string _stringValue;
      private int _intValue;
      private DateTime _dateTimeValue;

      public string StringValue
      {
        get { return _stringValue; }
        set { _stringValue = value; }
      }

      public int IntValue
      {
        get { return _intValue; }
        set { _intValue = value; }
      }

      public DateTime DateTimeValue
      {
        get { return _dateTimeValue; }
        set { _dateTimeValue = value; }
      }

      public TestToXmlDocument()
      {
      }

      public TestToXmlDocument(string stringValue, int intValue, DateTime dateTimeValue)
        : this()
      {
        _stringValue = stringValue;
        _intValue = intValue;
        _dateTimeValue = dateTimeValue;
      }
    }

    [Test]
    public void ToXmlDocumentTestObject()
    {
      TestToXmlDocument test = new TestToXmlDocument("Pie", int.MinValue, DateTime.MaxValue);

      XmlDocument doc = XmlUtils.ToXmlDocument(test);
      Assert.IsNotNull(doc);
      Assert.AreEqual(@"<TestToXmlDocument xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><StringValue>Pie</StringValue><IntValue>-2147483648</IntValue><DateTimeValue>9999-12-31T23:59:59.9999999</DateTimeValue></TestToXmlDocument>", doc.InnerXml);
    }
    #endregion
  }
}
